// Copyright (C) 2004-2009
// Alex Turbov, Igor Veresov
//
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/thread/unilock.hpp>

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

using namespace boost;

BOOST_AUTO_TEST_CASE(test_lock_matrix_1)
{
    typedef details::lock_matrix<exclusive_lock::type, exclusive_lock::matrix_type> matrix_t;
    matrix_t m;

    BOOST_CHECK_EQUAL(m.is_locked(exclusive_lock::lock), false);
    BOOST_CHECK_EQUAL(m.can_lock(exclusive_lock::lock), true);
    m.lock(exclusive_lock::lock);
    BOOST_CHECK_EQUAL(m.is_locked(exclusive_lock::lock), true);
    BOOST_CHECK_EQUAL(m.can_lock(exclusive_lock::lock), false);
    m.unlock(exclusive_lock::lock);
    BOOST_CHECK_EQUAL(m.is_locked(exclusive_lock::lock), false);
    BOOST_CHECK_EQUAL(m.can_lock(exclusive_lock::lock), true);
}

BOOST_AUTO_TEST_CASE(test_lock_matrix_2)
{
    typedef details::lock_matrix<rw_lock::type, rw_lock::matrix_type> matrix_t;
    matrix_t m;

    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::read), false);
    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::write), false);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::read), true);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::write), true);
    m.lock(rw_lock::read);
    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::read), true);
    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::write), false);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::read), true);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::write), false);
    m.unlock(rw_lock::read);
    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::read), false);
    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::write), false);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::read), true);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::write), true);
    m.lock(rw_lock::write);
    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::read), false);
    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::write), true);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::read), false);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::write), false);
    m.unlock(rw_lock::write);
    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::read), false);
    BOOST_CHECK_EQUAL(m.is_locked(rw_lock::write), false);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::read), true);
    BOOST_CHECK_EQUAL(m.can_lock(rw_lock::write), true);
}
